package com.jiuli.liping.util;




import com.jiuli.liping.vo.*;
import com.thoughtworks.xstream.XStream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class XmlUtil {
	// 所有需要注册的类的配置,
	//public static final String CONFIGFILE = new GetClassPath().getMyConfigPath()+ "/xml.prop";
	
	private static XStream xs = null;
	
	private static XmlUtil xmlUtil = null;

	private XmlUtil() {
		try {
			// 获取所有需要与处理的java完整名
			//List list = FileUtil.readFile(CONFIGFILE);
			List list =new ArrayList();
			list.add("com.jiuli.liping.vo.WorkflowRequestInfo");
			
			//初始化xs
			xs = alias(list);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得实例
	 * @return
	 */
	public static synchronized XmlUtil getInstance() {
		
		if(null == xmlUtil){
			xmlUtil = new XmlUtil();
		}
		
		return xmlUtil;
	}

	/**
	 * 根据类名做反射
	 * 
	 * @param list
	 *            类完整名
	 * @return
	 * @throws ClassNotFoundException
	 */
	public XStream alias(List list) throws ClassNotFoundException {
		XStream xs = new XStream();
		for (int i = 0; i < list.size(); i++) {
			Object obj;
			try {
				obj = Class.forName((String) list.get(i)).newInstance();
				Class zz = obj.getClass();
				aliasAtt(xs, zz);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return xs;
	}

	/**
	 * 对类进行xml解析配置
	 * 
	 * @param xs
	 * @param z
	 *            class对象
	 */
	public void aliasAtt(XStream xs, Class z) {
		if (null == z) {
			return;
		}

		// 类名注册
		xs.alias(z.getSimpleName(), z);
		Field[] field = z.getDeclaredFields();
		// 类中成员变量注册,继承的不注册
		for (int i = 0; i < field.length; i++) {
			xs.aliasField(field[i].getName(), z, field[i].getName());
		}
	}
	
	/**
	 * xml 格式字符串转换为对象
	 * @param str
	 * @return
	 */
	public Object xmlToObject(String str) {
		return xs.fromXML(str);
	}
	
	/**
	 * 对象转换为xml格式字符串
	 * @param obj
	 * @return
	 */
	public String objToXml(Object obj) {
		xs.alias("weaver.workflow.webservices.WorkflowDetailTableInfo", WorkflowDetailTableInfo.class);
		xs.alias("weaver.workflow.webservices.WorkflowRequestTableRecord", WorkflowRequestTableRecord.class);
		xs.alias("weaver.workflow.webservices.WorkflowRequestTableField", WorkflowRequestTableField.class);
		return xs.toXML(obj);
	}


//FileUtil.readFile()方法如下

/**
	 * 读取文件
	 * 
	 * @param filePath
	 *            文件完整名
	 * @return 文件内容
	 * @throws IOException
	 */
	public static List readFile(String filePath) {

		List list = new ArrayList();

		FileInputStream fs = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		try {
			fs = new FileInputStream(filePath);

			isr = new InputStreamReader(fs);

			br = new BufferedReader(isr);

			String data = "";
			while ((data = br.readLine()) != null) {
				list.add(data.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
				if (isr != null)
					isr.close();

				if (fs != null)
					fs.close();

			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return list;
	}
	
	public WorkflowRequestInfo getWorkflowRequestInfo (){
		//主字段
		WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[16]; //字段信息
		wrti[0] = new WorkflowRequestTableField(); 
		wrti[0].setFieldName("a1");//文本
		wrti[0].setFieldValue("1111");//被留言人字段的值，111为被留言人id
		wrti[0].setView(true);//字段是否可见
		wrti[0].setEdit(true);//字段是否可编辑
		
		wrti[1] = new WorkflowRequestTableField(); 
		wrti[1].setFieldName("a2");//数字
		wrti[1].setFieldValue("2222.22");
		wrti[1].setView(true);
		wrti[1].setEdit(true);

		wrti[2] = new WorkflowRequestTableField(); 
		wrti[2].setFieldName("a3");//日期
		wrti[2].setFieldValue("2013-05-15");
		wrti[2].setView(true);
		wrti[2].setEdit(true);
		
		wrti[3] = new WorkflowRequestTableField(); 
		wrti[3].setFieldName("a4");//单人力
		wrti[3].setFieldValue("82");
		wrti[3].setView(true);
		wrti[3].setEdit(true);	
	
		wrti[4] = new WorkflowRequestTableField(); 
		wrti[4].setFieldName("a5");//多请求
		wrti[4].setFieldValue("82,81");
		wrti[4].setView(true);
		wrti[4].setEdit(true);	
		
		wrti[5] = new WorkflowRequestTableField(); 
		wrti[5].setFieldName("a6");//单部门
		wrti[5].setFieldValue("3");
		wrti[5].setView(true);
		wrti[5].setEdit(true);	
		
		wrti[6] = new WorkflowRequestTableField(); 
		wrti[6].setFieldName("a7");//多部门
		wrti[6].setFieldValue("3,1");
		wrti[6].setView(true);
		wrti[6].setEdit(true);	
		
		wrti[7] = new WorkflowRequestTableField(); 
		wrti[7].setFieldName("a8");//单文档
		wrti[7].setFieldValue("3");
		wrti[7].setView(true);
		wrti[7].setEdit(true);	
		
		wrti[8] = new WorkflowRequestTableField(); 
		wrti[8].setFieldName("a9");//多文档
		wrti[8].setFieldValue("3,2");
		wrti[8].setView(true);
		wrti[8].setEdit(true);	
		
		wrti[9] = new WorkflowRequestTableField(); 
		wrti[9].setFieldName("a10");//单请求
		wrti[9].setFieldValue("79");
		wrti[9].setView(true);
		wrti[9].setEdit(true);	
		
		wrti[10] = new WorkflowRequestTableField(); 
		wrti[10].setFieldName("a11");//多请求
		wrti[10].setFieldValue("79,81");
		wrti[10].setView(true);
		wrti[10].setEdit(true);	
		
		wrti[11] = new WorkflowRequestTableField(); 
		wrti[11].setFieldName("a12");//附件(文件)
		wrti[11].setFieldType("http:附件网址.txt ,http:附件EntityService使用说明.docx ");//http:开头代表该字段为附件字段
		wrti[11].setFieldValue("http://192.168.4.204:86/testfile/网址.txt,http://192.168.4.204:86/testfile/EntityService使用说明.docx");//附件地址
		wrti[11].setView(true);
		wrti[11].setEdit(true);		
		
		wrti[12] = new WorkflowRequestTableField(); 
		wrti[12].setFieldName("a13");//附件(文件)
		wrti[12].setFieldType("http:附件2_wev8.jpg ,http:附件3_wev8.jpg ");//http:开头代表该字段为附件字段
		wrti[12].setFieldValue("http://192.168.4.204:86/testfile/2.jpg,http://192.168.4.204:86/testfile/3_wev8.jpg");//附件地址
		wrti[12].setView(true);
		wrti[12].setEdit(true);	
		
		wrti[13] = new WorkflowRequestTableField(); 
		wrti[13].setFieldName("a14");//选择框
		wrti[13].setFieldValue("0");
		wrti[13].setView(true);
		wrti[13].setEdit(true);	
		
		wrti[14] = new WorkflowRequestTableField(); 
		wrti[14].setFieldName("a15");//check框
		wrti[14].setFieldValue("1");
		wrti[14].setView(true);
		wrti[14].setEdit(true);	
		
		wrti[15] = new WorkflowRequestTableField(); 
		wrti[15].setFieldName("a16");//多行文本
		wrti[15].setFieldValue("1111");
		wrti[15].setView(true);
		wrti[15].setEdit(true);	
		



		
		WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];//主字段只有一行数据
		wrtri[0] = new WorkflowRequestTableRecord();
		wrtri[0].setWorkflowRequestTableFields(wrti);	
		
		WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
		wmi.setRequestRecords(wrtri);

		
		//明细字段
		WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[2];//两个明细表0明细表1,1明细表2
		//明细表1 start
		wrtri = new WorkflowRequestTableRecord[2];//数据 行数，假设添加2行明细数据
		//第一行
		wrti = new WorkflowRequestTableField[10]; //每行10个字段
		wrti[0] = new WorkflowRequestTableField(); 
		wrti[0].setFieldName("b1");//文本
		wrti[0].setFieldValue("bbbb1");
		wrti[0].setView(true);
		wrti[0].setEdit(true);	
		
		wrti[1] = new WorkflowRequestTableField(); 
		wrti[1].setFieldName("b2");//数字
		wrti[1].setFieldValue("2222.00");
		wrti[1].setView(true);
		wrti[1].setEdit(true);

		wrti[2] = new WorkflowRequestTableField(); 
		wrti[2].setFieldName("b3");//单人力
		wrti[2].setFieldValue("82");
		wrti[2].setView(true);
		wrti[2].setEdit(true);
		
		wrti[3] = new WorkflowRequestTableField(); 
		wrti[3].setFieldName("b4");//多人力
		wrti[3].setFieldValue("82,34");
		wrti[3].setView(true);
		wrti[3].setEdit(true);
		
		wrti[4] = new WorkflowRequestTableField(); 
		wrti[4].setFieldName("b5");//多文档
		wrti[4].setFieldValue("3");
		wrti[4].setView(true);
		wrti[4].setEdit(true);
		
		wrti[5] = new WorkflowRequestTableField(); 
		wrti[5].setFieldName("b6");//多请求
		wrti[5].setFieldValue("82,81");
		wrti[5].setView(true);
		wrti[5].setEdit(true);
		
		wrti[6] = new WorkflowRequestTableField(); 
		wrti[6].setFieldName("b7");//选择框
		wrti[6].setFieldValue("0");
		wrti[6].setView(true);
		wrti[6].setEdit(true);
		
		wrti[7] = new WorkflowRequestTableField(); 
		wrti[7].setFieldName("b8");//check框
		wrti[7].setFieldValue("1");
		wrti[7].setView(true);
		wrti[7].setEdit(true);
		
		wrti[8] = new WorkflowRequestTableField(); 
		wrti[8].setFieldName("b9");//多行文本
		wrti[8].setFieldValue("bbbb1");
		wrti[8].setView(true);
		wrti[8].setEdit(true);
		
		wrti[9] = new WorkflowRequestTableField(); 
		wrti[9].setFieldName("b10");//日期
		wrti[9].setFieldValue("2013-05-15");
		wrti[9].setView(true);
		wrti[9].setEdit(true);
		
		
		wrtri[0] = new WorkflowRequestTableRecord();
		wrtri[0].setWorkflowRequestTableFields(wrti);
		
		//第二行
		wrti = new WorkflowRequestTableField[10]; //每行3个字段
		wrti[0] = new WorkflowRequestTableField(); 
		wrti[0].setFieldName("b1");//文本
		wrti[0].setFieldValue("bbbb1");
		wrti[0].setView(true);
		wrti[0].setEdit(true);	
		
		wrti[1] = new WorkflowRequestTableField(); 
		wrti[1].setFieldName("b2");//数字
		wrti[1].setFieldValue("2222.00");
		wrti[1].setView(true);
		wrti[1].setEdit(true);

		wrti[2] = new WorkflowRequestTableField(); 
		wrti[2].setFieldName("b3");//单人力
		wrti[2].setFieldValue("82");
		wrti[2].setView(true);
		wrti[2].setEdit(true);
		
		wrti[3] = new WorkflowRequestTableField(); 
		wrti[3].setFieldName("b4");//多人力
		wrti[3].setFieldValue("82,34");
		wrti[3].setView(true);
		wrti[3].setEdit(true);
		
		wrti[4] = new WorkflowRequestTableField(); 
		wrti[4].setFieldName("b5");//多文档
		wrti[4].setFieldValue("3");
		wrti[4].setView(true);
		wrti[4].setEdit(true);
		
		wrti[5] = new WorkflowRequestTableField(); 
		wrti[5].setFieldName("b6");//多请求
		wrti[5].setFieldValue("82,81");
		wrti[5].setView(true);
		wrti[5].setEdit(true);
		
		wrti[6] = new WorkflowRequestTableField(); 
		wrti[6].setFieldName("b7");//选择框
		wrti[6].setFieldValue("0");
		wrti[6].setView(true);
		wrti[6].setEdit(true);
		
		wrti[7] = new WorkflowRequestTableField(); 
		wrti[7].setFieldName("b8");//check框
		wrti[7].setFieldValue("1");
		wrti[7].setView(true);
		wrti[7].setEdit(true);
		
		wrti[8] = new WorkflowRequestTableField(); 
		wrti[8].setFieldName("b9");//多行文本
		wrti[8].setFieldValue("bbbb1");
		wrti[8].setView(true);
		wrti[8].setEdit(true);
		
		wrti[9] = new WorkflowRequestTableField(); 
		wrti[9].setFieldName("b10");//日期
		wrti[9].setFieldValue("2013-05-15");
		wrti[9].setView(true);
		wrti[9].setEdit(true);
		
		wrtri[1] = new WorkflowRequestTableRecord();
		wrtri[1].setWorkflowRequestTableFields(wrti);
		
		wdti[0] = new WorkflowDetailTableInfo();
		wdti[0].setWorkflowRequestTableRecords(wrtri);//加入明细表1的数据
		//String tableFieldName[]= {"formtable_main_3_dt1"};
		//wdti[0].setTableFieldName(tableFieldName);
		//明细表1 end
		
		
		//明细表2 start
		wrtri = new WorkflowRequestTableRecord[2];//数据行数，假设添加1行明细数据
		
		//第一行
		wrti = new WorkflowRequestTableField[10]; //每行10个字段
		wrti[0] = new WorkflowRequestTableField(); 
		wrti[0].setFieldName("c1");//文本
		wrti[0].setFieldValue("bbbb1");
		wrti[0].setView(true);
		wrti[0].setEdit(true);	
		
		wrti[1] = new WorkflowRequestTableField(); 
		wrti[1].setFieldName("c2");//数字
		wrti[1].setFieldValue("2222.00");
		wrti[1].setView(true);
		wrti[1].setEdit(true);

		wrti[2] = new WorkflowRequestTableField(); 
		wrti[2].setFieldName("c3");//单人力
		wrti[2].setFieldValue("82");
		wrti[2].setView(true);
		wrti[2].setEdit(true);
		
		wrti[3] = new WorkflowRequestTableField(); 
		wrti[3].setFieldName("c4");//多人力
		wrti[3].setFieldValue("82,34");
		wrti[3].setView(true);
		wrti[3].setEdit(true);
		
		wrti[4] = new WorkflowRequestTableField(); 
		wrti[4].setFieldName("c5");//多文档
		wrti[4].setFieldValue("3");
		wrti[4].setView(true);
		wrti[4].setEdit(true);
		
		wrti[5] = new WorkflowRequestTableField(); 
		wrti[5].setFieldName("c6");//多请求
		wrti[5].setFieldValue("82,81");
		wrti[5].setView(true);
		wrti[5].setEdit(true);
		
		wrti[6] = new WorkflowRequestTableField(); 
		wrti[6].setFieldName("c7");//选择框
		wrti[6].setFieldValue("0");
		wrti[6].setView(true);
		wrti[6].setEdit(true);
		
		wrti[7] = new WorkflowRequestTableField(); 
		wrti[7].setFieldName("c8");//check框
		wrti[7].setFieldValue("1");
		wrti[7].setView(true);
		wrti[7].setEdit(true);
		
		wrti[8] = new WorkflowRequestTableField(); 
		wrti[8].setFieldName("c9");//多行文本
		wrti[8].setFieldValue("bbbb1");
		wrti[8].setView(true);
		wrti[8].setEdit(true);
		
		wrti[9] = new WorkflowRequestTableField(); 
		wrti[9].setFieldName("c10");//日期
		wrti[9].setFieldValue("2013-05-15");
		wrti[9].setView(true);
		wrti[9].setEdit(true);

		wrtri[0] = new WorkflowRequestTableRecord();
		wrtri[0].setWorkflowRequestTableFields(wrti);
		
		//第二行
		wrti = new WorkflowRequestTableField[10]; //每行10个字段
		wrti[0] = new WorkflowRequestTableField(); 
		wrti[0].setFieldName("c1");//文本
		wrti[0].setFieldValue("bbbb1");
		wrti[0].setView(true);
		wrti[0].setEdit(true);	
		
		wrti[1] = new WorkflowRequestTableField(); 
		wrti[1].setFieldName("c2");//数字
		wrti[1].setFieldValue("2222.00");
		wrti[1].setView(true);
		wrti[1].setEdit(true);

		wrti[2] = new WorkflowRequestTableField(); 
		wrti[2].setFieldName("c3");//单人力
		wrti[2].setFieldValue("82");
		wrti[2].setView(true);
		wrti[2].setEdit(true);
		
		wrti[3] = new WorkflowRequestTableField(); 
		wrti[3].setFieldName("c4");//多人力
		wrti[3].setFieldValue("82,34");
		wrti[3].setView(true);
		wrti[3].setEdit(true);
		
		wrti[4] = new WorkflowRequestTableField(); 
		wrti[4].setFieldName("c5");//多文档
		wrti[4].setFieldValue("3");
		wrti[4].setView(true);
		wrti[4].setEdit(true);
		
		wrti[5] = new WorkflowRequestTableField(); 
		wrti[5].setFieldName("c6");//多请求
		wrti[5].setFieldValue("82,81");
		wrti[5].setView(true);
		wrti[5].setEdit(true);
		
		wrti[6] = new WorkflowRequestTableField(); 
		wrti[6].setFieldName("c7");//选择框
		wrti[6].setFieldValue("0");
		wrti[6].setView(true);
		wrti[6].setEdit(true);
		
		wrti[7] = new WorkflowRequestTableField(); 
		wrti[7].setFieldName("c8");//check框
		wrti[7].setFieldValue("1");
		wrti[7].setView(true);
		wrti[7].setEdit(true);
		
		wrti[8] = new WorkflowRequestTableField(); 
		wrti[8].setFieldName("c9");//多行文本
		wrti[8].setFieldValue("bbbb1");
		wrti[8].setView(true);
		wrti[8].setEdit(true);
		
		wrti[9] = new WorkflowRequestTableField(); 
		wrti[9].setFieldName("c10");//日期
		wrti[9].setFieldValue("2013-05-15");
		wrti[9].setView(true);
		wrti[9].setEdit(true);

		wrtri[1] = new WorkflowRequestTableRecord();
		wrtri[1].setWorkflowRequestTableFields(wrti);		
		
		
		wdti[1] = new WorkflowDetailTableInfo();
		wdti[1].setWorkflowRequestTableRecords(wrtri);//加入明细表2的数据
		//String tableFieldName1[]= {"formtable_main_3_dt2"};
		//wdti[1].setTableFieldName(tableFieldName1);
		//明细表2 end
		
		//流程基本信息
		WorkflowBaseInfo wbi = new WorkflowBaseInfo();
		wbi.setWorkflowId("61");//workflowid 61 代表 webservices接口测试流程
		
		//流程明细表信息
		
		
	    WorkflowRequestInfo wri = new WorkflowRequestInfo();//流程基本信息	
		wri.setCreatorId("82");//创建人id
		wri.setRequestLevel("2");//0 正常，1重要，2紧急
		wri.setRequestName("webservices接口测试流程-罗燕-2013-05-15_002");//流程标题
	    wri.setWorkflowMainTableInfo(wmi);//添加主字段数据
		wri.setWorkflowBaseInfo(wbi);//流程基本信息
		wri.setWorkflowDetailTableInfos(wdti);//添加明细字段数据
		
		return wri;
	}		

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			XmlUtil xmlUtil = XmlUtil.getInstance();
			
			WorkflowRequestInfo wri = xmlUtil.getWorkflowRequestInfo();
	     	//List list = FileUtil.readFile(XmlUtil.CONFIGFILE);
	     	
	     	String xml = xmlUtil.objToXml(wri);//对象至xml
	     	System.out.println(xml);
	     	WorkflowRequestInfo obj = (WorkflowRequestInfo)xmlUtil.xmlToObject(xml);//xml至对象	
	     	System.out.println(obj.getRequestName());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
   
	}	
}